package com.turing.data.scripts.contract.filter

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.TypeReference
import com.turing.common.entity.TextVO
import com.turing.common.util.FileUtils
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang3.SerializationUtils
import com.turing.data.engine.utils.RegexExtract;

/**
 * 场景：资产管理合同
 * 版本：Version 1
 * 要素名称：是否涉及境外投资
 */

class IfOutlands_30174_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){
        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()

        def REGEX_BACKUP_1 = "权益类资产"
        def textMap = dependParams.textMap
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null != childTaskSn || childTaskSn != ""){
                    def start = drawResultVO.start
                    def end = drawResultVO.end
                    def text = drawResultVO.text
                    text = text.replaceAll("\n","")
                    def textPathMap = dependParams.textPathMap

                    def rawText = textMap.get(drawResultVO.childTaskSn)
                    def expandOffset = 0
                    //往前30个字符搜索（同时匹配两个关键字才返回）
                    while( expandOffset < 60 && start > expandOffset ){
                        def expandString = rawText.substring(start-expandOffset,start)
                        if( expandString =~ REGEX_BACKUP_1) {
                            trueValueSet.add(drawResultVO)
                            return trueValueSet
                        }
                        expandOffset ++
                    }
                }
            }
        }

        try{
            if(trueValueSet.size()==0){
                String REGEX0 = /(沪港通.{1,10}深港通|深港通.{1,10}沪港通|沪港通|深港通)/
                dependParams.JSONTextMap.forEach(
                        { childTaskSn, jsonText ->
                            RegexExtract.doRuleExtract(REGEX0,childTaskSn, dependParams, jsonText, trueValueSet)
                        }
                )

                if(trueValueSet.size()>0){
                    return trueValueSet
                }
            }
        }
        catch (Exception e){
            e.printStackTrace()
        }

        TaskElementVO.DrawResultVO drawResultVO = new TaskElementVO.DrawResultVO()
        drawResultVO.businessValue = "否"
        drawResultVO.text = ""
        drawResultVO.setType(1)
        trueValueSet.add(drawResultVO)
        return trueValueSet
    }

    public static void main(String[] args) {

        String jsonText = FileUtils.loadJson("http://192.168.1.73:7003/group1/M00/02/96/wKgBSWJFGDuAe8JjACiIUv3FMa003.json");
        List<TextVO> textVOS = JSON.parseObject(jsonText, new TypeReference<ArrayList<TextVO>>() {});
        println textVOS.toString()

    }

}
